<script>
export default {
  name: "GoodsList",
  methods:{
    joinCart(goods) {
      if (localStorage.getItem("token")) {
        this.$store.commit('JOIN_CART', goods);
        this.$router.push("/myCart");
      } else {
        // 未登陆，跳转至登陆界面
        this.$router.push({path: "/login", query: {returnUrl: this.$route.fullPath}})
      }
    }
  }
}
</script>

<template>
  <div>
    <h3 v-if="$store.state.goods.goodsList.length===0">暂无商品</h3>
    <template v-else>
       <div v-for="item in $store.state.goods.goodsList" :key="item.id">
            <p>商品名称:{{item.goodsName}}</p>
            <p>商品价格：{{item.goodsPrice | currency}}</p>
            <p>上架时间：{{item.addTime | date}}</p>
            <button @click="joinCart(item)">加入购物车</button>
       </div>
    </template>
  </div>


</template>

<style scoped>

</style>